﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Results;
using PH.Model.Commons;
using PH.Logic.Commons;
namespace PH.Web.Management.Controllers
{
    [RouteArea("Files")]
    public class FileController : Controller
    {
        [Route("ExcelUpLoad")]
        public ActionResult ExcelUpLoad()
        {
            string errinfo = "error";
            try
            {
                if (Request.Files != null && Request.Files.Count > 0)
                {
                    var file = Request.Files[0];
                    if (file.ContentLength > 0)
                    {
                        var allowExt = new string[] { ".xls", ".xlsx" };
                        var ext = System.IO.Path.GetExtension(file.FileName).ToLower();
                        if (allowExt.Contains(ext))
                        {
                            //var relaPath = string.Format("/RootFiles/UpLoad/{0:yyyy-MM}/", DateTime.Now);
                            //var realPath = Server.MapPath(relaPath);
                            //var filename = string.Format("{0:yyyyMMddHHmmssfff}{1}", DateTime.Now, ext);
                            //if (!System.IO.Directory.Exists(realPath)) System.IO.Directory.CreateDirectory(realPath);
                            //file.SaveAs(realPath + filename);

                            var buffer = new byte[file.ContentLength];
                            file.InputStream.Read(buffer, 0, buffer.Length);
                            var mFile = new FileModel()
                            {
                                LocalName = file.FileName,
                                Size = file.ContentLength, 
                                Data=buffer,
                                FType=1
                            };
                            FileLogic.CreateNewFile(mFile);
                            return Json(new JsonResult<Model.Commons.FileModel>(mFile), "text/html");
                        }
                        else
                            errinfo = "不支持的文件格式";
                    }
                    else
                        errinfo = "无效的文件大小";
                }
                else
                    errinfo = "无上传文件";
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }

            return Json(new JsonResult<Model.Commons.FileModel>(null, errinfo, false), "text/html");
            //为了兼容ie10以下的浏览器，需加入text/html的content-type
        }

        [Route("DownLoad/{fileID:guid}")]
        public ActionResult DownLoad(Guid fileID)
        {
            if (fileID!=Guid.Empty)
            {
                var file = FileLogic.GetFile(fileID);
                if (file != null)
                {
                    return File(file.Data, "application/octet-stream", file.LocalName);
                }
            }
            return Content("没有文件");
        }
    }
}
